﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class OperatorClients : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Roles.IsUserInRole("Operator"))
        {
            Response.Redirect("~/Account/Login.aspx");
        }
        else
        {
            SqlDataSourceClientsList.SelectParameters["OperatorId"].DefaultValue = getOperatorId().ToString();
        }
    }

    protected int getOperatorId()
    {
        var operators = from bankOperator in db.Operators where bankOperator.UserId == (Guid)Membership.GetUser().ProviderUserKey select bankOperator;
        return operators.First().OperatorId;
    }

    protected void AddClient_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Response.Redirect("~/Operator/ClientsCreateEdit.aspx");
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string companyId = GridView1.DataKeys[GridView1.SelectedIndex]["CompanyId"].ToString();
        var profiles = from profile in db.UserProfiles where profile.CompanyId == Convert.ToInt32(companyId) select profile;
        Session.Add("companyId", companyId);
        Session.Add("profileId", profiles.First().ProfileId.ToString());
        Response.Redirect("~/Operator/ClientsCreateEdit.aspx");
    }

    protected void ClickAddRequestCredit(object sender, EventArgs e)
    {
        LinkButton linkButton = (LinkButton)sender;
        Session.Add("clientId", linkButton.CommandArgument);
        Response.Redirect("~/RequestCredit.aspx");
    }

    protected void ClickShowAccounts(object sender, EventArgs e)
    {
        LinkButton linkButton = (LinkButton)sender;
        Session.Add("companyId", linkButton.CommandArgument);
        Response.Redirect("~/Operator/BankAccounts.aspx");
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            HyperLink link = (HyperLink)e.Row.FindControl("AddCreditLink");
            LinkButton showAccountsLinkButton = (LinkButton)e.Row.FindControl("ShowAccountsLinkButton");
            LinkButton requestCreditLinkButton = (LinkButton)e.Row.FindControl("RequestCreditLinkButton");
            string companyId = GridView1.DataKeys[e.Row.RowIndex]["CompanyId"].ToString();
            var profiles = from profile in db.UserProfiles where profile.CompanyId == Convert.ToInt32(companyId) select profile;
            requestCreditLinkButton.CommandArgument = profiles.First().ProfileId.ToString();
            showAccountsLinkButton.CommandArgument = companyId;
        }
    }
    protected void GridView1_DataBinding(object sender, EventArgs e)
    {
        SqlDataSourceClientsList.SelectParameters["OperatorId"].DefaultValue = getOperatorId().ToString();
    }
}